// Copyright 2006 Two Pair, LLC. All Rights Reserved.

/**
 * An abstract interface for receiving the output of
 * {@link WordLexer#processInput(InputStream, WordParser)}. Each word in the
 * input is passed into {@link #processWord(String)} sequentially.
 */
public interface WordParser {

  /**
   * A collection of helper methods for use by implementors of
   * {@link WordParser}.
   */
  class Util {

    /**
     * A fast-lookup array that determines whether a character is upper case.
     */
    private static final boolean[] IS_UPPER_CASE = new boolean[256];

    /**
     * The amount that must be added to an upper case character to make it lower
     * case.
     */
    private static final int TOLOWER_DELTA = 'a' - 'A';

    /**
     * Compute the lower case version of a word.
     * 
     * @param word the word to convert
     * @return the lower case version of the word
     */
    public static String toLowerCase(String word) {
      char[] input = word.toCharArray();
      boolean changed = false;
      for (int i = 0, len = input.length; i < len; ++i) {
        if (IS_UPPER_CASE[input[i]]) {
          changed = true;
          input[i] = (char) (input[i] + TOLOWER_DELTA);
        }
      }
      if (changed) {
        // return a new string based on the input
        return String.valueOf(input);
      }
      // just use the original
      return word;
    }

    static {
      // Initialize the upper case lookup array
      for (char i = 'A'; i <= 'Z'; ++i) {
        IS_UPPER_CASE[i] = true;
      }
    }

  }

  /**
   * Processes the next word in the input.
   * 
   * @param word the next word to process
   */
  void processWord(String word);

}
